<template>
  <div class="upload-container">
    <input
        type="file"
        ref="fileInput"
        @change="handleFileChange"
        class="file-input"
    />
    <button @click="uploadFile" class="upload-button">上传图片</button>
    <div id="content" class="content">
      <span v-for="(line, index) in content" :key="index" class="content-line">
        {{ line }}
      </span>
    </div>
  </div>
</template>

<script lang="ts">
import { ref } from "vue";

export default {
  setup() {
    const fileInput = ref(null);
    const content = ref<string[]>([]); // 使用数组来存储逐步更新的内容

    // 处理文件选择
    const handleFileChange = () => {
      if (!fileInput.value?.files?.length) {
        alert("请选择文件");
        return;
      }
    };

    // 上传文件
    const uploadFile = async () => {
      if (!fileInput.value?.files?.length) {
        alert("请选择文件");
        return;
      }
      let formData = new FormData();
      formData.append("file", fileInput.value.files[0]);
      // 清空当前显示的内容
      content.value = [];
      try {
        // 1. 上传文件
        const uploadResponse = await fetch("/api/fileController/upload", {
          method: "POST",
          body: formData,
        });

        if (!uploadResponse.ok) {
          throw new Error("上传失败");
        }
        const uploadData = await uploadResponse.json();
        alert("上传成功");

        // 2. 获取图片识别流数据
        const recognitionResponse = await fetch(`/api/pictureRec/imageRecognition?fileName=${uploadData.data}`);
        if (!recognitionResponse.ok) {
          throw new Error("图片识别失败");
        }

        // 获取流数据
        const reader = recognitionResponse.body?.getReader();
        if (!reader) {
          throw new Error("无法读取流数据");
        }

        const decoder = new TextDecoder("utf-8");
        let chunk = "";
        let done = false;

        // 逐步读取数据
        while (!done) {
          const { value, done: readerDone } = await reader.read();
          done = readerDone;
          chunk = decoder.decode(value, { stream: true });
          content.value.push(chunk); // 每读取到一块数据就推入数组
        }
      } catch (error) {
        console.error(error);
        alert("上传或识别失败");
      }
    };

    return {
      fileInput,
      content,
      handleFileChange,
      uploadFile,
    };
  },
};
</script>

<style scoped>
/* Container */
.upload-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  max-width: 500px;
  margin: 0 auto;
}

.file-input {
  padding: 10px;
  margin-bottom: 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.upload-button {
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.upload-button:hover {
  background-color: #45a049;
}

.content {
  margin-top: 20px;
  padding: 10px;
  width: 100%;
  background-color: #fff;
  border-radius: 4px;
  border: 1px solid #ccc;
  max-height: 300px;
  overflow-y: auto;
  white-space: normal; /* Allow text to wrap */
  word-wrap: break-word; /* Break long words */
}

.content-line {
  font-size: 14px;
  color: #333;
  margin-right: 10px; /* Space between lines */
  display: inline-block; /* Make lines in a single row */
  line-height: 1.5;
}
</style>